package com.zb.mvc.controller.base;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.write.WriteException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zb.entity.base.JsonCommon;
import com.zb.entity.base.JsonForList;
import com.zb.entity.charger.OmsChargerErrorRecord;
import com.zb.service.base.ChargerErrorRecordService;
import com.zb.util.ActionUtils;
import com.zb.util.ExcelExporter;
import com.zb.util.StringUtil;

import java.beans.IntrospectionException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

/**
 * 
 * @author huanghao
 *
 */
@Controller
@RequestMapping("/chargerErrorRecordApi")
public class ChargerErrorRecordAction extends BaseAction
{
	@Autowired
	private ChargerErrorRecordService chargerErrorRecordServiceImpl;
	@RequestMapping(value="add",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> add(HttpServletRequest request, HttpServletResponse response)
	{
		return chargerErrorRecordServiceImpl.add(this.getObjFromRequest(request,true,false));
	}
	
	@RequestMapping(value="update/{id}",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> update(@PathVariable(value="id") String id,
			HttpServletRequest request, HttpServletResponse response)
	{
		OmsChargerErrorRecord stationCorrectList = this.getObjFromRequest(request,false,false);
		stationCorrectList.setCharger_error_id(Long.parseLong(id));
		return chargerErrorRecordServiceImpl.update(stationCorrectList);
	}
	
	@RequestMapping(value="delete/{id}",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> delete(@PathVariable(value="id") String id,
			HttpServletRequest request, HttpServletResponse response)
	{
		return chargerErrorRecordServiceImpl.delete(id);
	}
	
	@RequestMapping(value="findAllOrPage",method=RequestMethod.GET)
	@ResponseBody
	public JsonForList<?> findAllOrPage(HttpServletRequest request, HttpServletResponse response)
	{
		return chargerErrorRecordServiceImpl.findPageObjs(this.buildQueryParam(request, true), this.getObjFromRequest(request,false,true));
	}
	
	@RequestMapping(value="findAllOrPage2Excel",method=RequestMethod.GET)
	public void findAllOrPage2Excel(HttpServletRequest request, HttpServletResponse response) throws WriteException, IOException, IllegalAccessException, IntrospectionException, InvocationTargetException
	{
		JsonForList<?> lst = chargerErrorRecordServiceImpl.findPageObjs(this.buildQueryParam(request, true), this.getObjFromRequest(request,false,true));
		String[] title = new String[]{"ID","设备地址ID","处理时间","","","","",};
		String fileName = StringUtil.now14time()+"_ChargerError.xls";
//		ExcelExporter.exportExcel(fileName, title, (List<Object>) lst.getList(), response); TODO luoyp
		ExcelExporter e = new ExcelExporter();
		e.export(lst.getList(), response, "123.xls");
	}
	
	@RequestMapping(value="findObj/{id}",method=RequestMethod.GET)
	@ResponseBody
	public JsonCommon<?> findObj(@PathVariable(value="id") String id,
			HttpServletRequest request, HttpServletResponse response)
	{
		return chargerErrorRecordServiceImpl.findById(id);
	}
	
	private OmsChargerErrorRecord getObjFromRequest(HttpServletRequest request,boolean isCreate,boolean isFind)
	{
		String loginUserId = this.getLoginUserId(request);
		return ActionUtils.getParamInstance(OmsChargerErrorRecord.class, ActionUtils.getParameterMap(request),loginUserId,isCreate,isFind);
	}
}
